package com.study_day01.service;

import com.study_day01.entiry.CartItem;
import com.study_day01.entiry.Drug;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CartService {
    @Autowired
    private DrugService drugService;
    private Map<Integer, CartItem> cartItems = new HashMap<>();

    public void addToCart(int drId) {
        if (cartItems.containsKey(drId)) {
            CartItem item = cartItems.get(drId);
            item.setQuantity(item.getQuantity() + 1);
        } else {
            Drug drug = drugService.getDrugById(drId);
            if (drug != null) {
                CartItem item = new CartItem(drug);
                cartItems.put(drId, item);
            }
        }
    }

    public List<CartItem> getCartItems() {
        return new ArrayList<>(cartItems.values());
    }

    public void removeFromCart(int drId) {
        cartItems.remove(drId);
    }
}
